package com.cardbaobao.cardbabyclient.ui.activity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.base.BaseResult;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.buried.SelfUtils;
import com.cardbaobao.cardbabyclient.model.Event;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.ui.pop.RepayNumberKeyBoardPop;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.utils.JsonUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.widget.CustomSetPwdView;
import com.cardbaobao.cardbabyclient.widget.TitleBar;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Description: 设置交易密码
 * Created by FHW
 * on 2017/11/10
 */

public class SetTradePwdActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    TitleBar title_bar;

    @BindView(R.id.v_pwd)
    CustomSetPwdView v_pwd;
    @BindView(R.id.tv_content)
    TextView tv_content;
    @BindView(R.id.tv_content_pro)
    TextView tv_content_pro;

    private int type = 0;//0-设置 1- 修改

    private RepayNumberKeyBoardPop pop;
    private String tranKey = "";
    private String tranKeyOne = "";
    private boolean isFinish = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_trade_pwd;
    }

    @OnClick({R.id.v_pwd})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.v_pwd:
                showPopKeyboard();
                break;
        }
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        mCurrentPageId= Self.Page.app_set_pay_password;

        title_bar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mCurrentPageId=Self.Page.app_modify_pay_pwd_page;

        type = getIntent().getIntExtra("type", 0);
        switch (type) {
            case 0:
                title_bar.setTitleText("设置支付密码");
                break;
            case 1:
                title_bar.setTitleText("修改支付密码");
                break;
        }

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (hasFocus) {
            showPopKeyboard();
        }
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
    }

    private void showPopKeyboard() {
        if (pop == null) {
            pop = new RepayNumberKeyBoardPop(mContext);
            pop.setNumberKeyListener(new RepayNumberKeyBoardPop.OnKeyPressed() {
                @Override
                public void onKeyPressed(int key) {
                    isFinish = false;
                    StringBuilder sb = new StringBuilder(tranKey);
                    switch (key) {
                        case RepayNumberKeyBoardPop.OnKeyPressed.KEY_FINISH:
                            //完成
                            isFinish = true;
                            break;
                        case RepayNumberKeyBoardPop.OnKeyPressed.KEY_DELETE:
                            //删除键
                            if (sb.length() > 0) {
                                sb.deleteCharAt(sb.length() - 1);
                            }
                            break;
                        case RepayNumberKeyBoardPop.OnKeyPressed.KEY_ONE:
                            //数字键1
                        case RepayNumberKeyBoardPop.OnKeyPressed.KEY_TWO:
                            //数字键2
                        case RepayNumberKeyBoardPop.OnKeyPressed.KEY_THREE:
                            //数字键3
                        case RepayNumberKeyBoardPop.OnKeyPressed.KEY_FOUR:
                            //数字键4
                        case RepayNumberKeyBoardPop.OnKeyPressed.KEY_FIVE:
                            //数字键5
                        case RepayNumberKeyBoardPop.OnKeyPressed.KEY_SIX:
                            //数字键6
                        case RepayNumberKeyBoardPop.OnKeyPressed.KEY_SEVEN:
                            //数字键7
                        case RepayNumberKeyBoardPop.OnKeyPressed.KEY_EIGHT:
                            //数字键8
                        case RepayNumberKeyBoardPop.OnKeyPressed.KEY_NINE:
                            //数字键9
                        case RepayNumberKeyBoardPop.OnKeyPressed.KEY_ZERO:
                            //数字键0
                            sb.append(key);
                            break;
                    }
                    //限制6位验证码
                    if (sb.length() > 6) {
                        tranKey = sb.substring(0, 6);
                    } else {
                        tranKey = sb.toString();
                    }
//                LogUtil.d(tranKey);
                    if (tranKey.length() >= 6) {
                        if (tranKeyOne.equals("")) {//第一次密码
                            tranKeyOne = tranKey;
                            tranKey = "";
                            tv_content.setTextColor(getResources().getColor(R.color.color_999999));
                            tv_content.setText("请再次输入6位数字交易密码");
                            tv_content_pro.setVisibility(View.GONE);
                        } else {//第二次密码
                            if (tranKey.equals(tranKeyOne)) {
                                if (isFinish) {
                                    SelfUtils.buriedClickData(Self.Action.app_click_modify_payment_pwd,
                                            Self.Action.app_click_modify_payment_pwd_des,
                                            mEnterPageTime,
                                            mCurrentPageId,
                                            mPrePageId);
                                    setRepayPwd();
                                    pop.dismiss();
                                }
                            } else {//两次不一样
                                tranKey = "";
                                tranKeyOne = "";
//                                tv_content.setTextColor(getResources().getColor(R.color.color_FE1211));
//                                tv_content.setText("两次密码不一致");
                                tv_content.setTextColor(getResources().getColor(R.color.color_999999));
                                tv_content.setText("设置6位数字支付密码");
                                ToastUtil.showToast("两次密码不一致");
                                tv_content_pro.setVisibility(View.VISIBLE);
                            }
                        }
                    }
                    if (tranKey.length() > 0) {
                        v_pwd.setShowNum(tranKey.length());
                    } else {
                        v_pwd.setShowNum(0);
                    }

                }
            });
        }
        if (!pop.isShowing()) {
            pop.showAtLocation(v_pwd, Gravity.BOTTOM, 0, 0);
        }
    }

    /**
     * 设置或修改密码
     */
    private void setRepayPwd() {
        showProgressDialog(false);
        Api.setRepayPwd(mApplication.getUser().getID() + "", tranKeyOne)
//        Api.setRepayPwd("5001766", tranKeyOne)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissProgressDialog();
                        ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                                getString(R.string.string_request_server_fail));
                    }

                    @Override
                    public void onNext(String s) {
                        dismissProgressDialog();
                        BaseResult baseResult = JsonUtil.json2Object(s, BaseResult.class);
                        if (baseResult.getCode() == Constant.SUC_CODE) {
                            EventBus.getDefault().post(new Event.SetRepayPwdSuc());
                            setResult(Constant.RESULT_CODE);
                            finish();
                        } else {
                            ToastUtil.showToast(baseResult.getMsg());
                        }

                    }
                });
    }

}
